package com.example.reschain.demo02;

import com.example.reschain.demo02.service.BankOrderHandleInterceptService;
import com.example.reschain.demo02.service.RepeatOrderHandleInterceptService;
import com.example.reschain.demo02.service.ValidOrderHandleInterceptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author renyitong
 * @version 1.0.0
 * @createTime 2024年10月09日
 * @Description
 */
@Component
public class OrderHandleChainService2 {

    private final List<OrderHandleIntercept> handleList = new ArrayList<>();

    @Autowired
    private RepeatOrderHandleInterceptService repeatOrderHandleInterceptService;

    @Autowired
    private ValidOrderHandleInterceptService validOrderHandleInterceptService;

    @Autowired
    private BankOrderHandleInterceptService bankOrderHandleInterceptService;

    @PostConstruct
    public void init() {
        // 依次手动add对象
        handleList.add(repeatOrderHandleInterceptService);
        handleList.add(validOrderHandleInterceptService);
        handleList.add(bankOrderHandleInterceptService);
    }

    /**
     * 执行处理
     *
     * @param context
     * @return
     */
    public OrderContext execute(OrderContext context) {
        for (OrderHandleIntercept handleIntercept : handleList) {
            context = handleIntercept.handle(context);
        }
        return context;
    }


}
